﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JiZiYan.Controllers
{
    /// <summary>
    /// 角色管理页面
    /// </summary>
    public class RolesController : Controller
    {
        // GET: Roles
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 分页方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int page, limit;
            if (!int.TryParse(Request.Params["page"], out page))
            {
                page = 1;
            }
            if (!int.TryParse(Request.Params["limit"], out limit))
            {
                limit = 10;
            }
            
            JiZiYanEntities2 db = new JiZiYanEntities2();
            var arr = db.Roles.Where(x => true);            
            var count = arr.Count();
            var data = arr.OrderByDescending(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList();
            return Json(new { code = 0, count = count, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改角色页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Updata(Guid id)
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            {
                var thisid = db.Roles.SingleOrDefault(x => x.Id == id);
                ViewBag.thisid = thisid;
                //查询当前角色的id
                var thisroles = db.RoleModules.Where(x => x.RoleId == thisid.Id).ToList();
                //查询当前角色的访问页面id               
                var allModules = db.Modules.ToList();
                List<Modules> m = new List<Modules>();
                for (int i = 0; i < thisroles.Count(); i++)
                {
                    var thisrolesname = thisroles[i].ModuleId;
                    var callname = db.Modules.SingleOrDefault(x => x.Id == thisrolesname);
                    m.Add(callname);
                    //所有能看到的页面
                    allModules.Remove(callname);
                    //看不到的页面
                }
                ViewBag.yes = m;    //当前能看到的页面
                ViewBag.no = allModules;    //当前不能看到的页面
            }                
            return View();
        }
        /// <summary>
        /// 修改角色方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Updata(Guid Id, string Name,List<int>ModuleId)
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            var roles = db.Roles.SingleOrDefault(x => x.Id == Id);
            //得到当前角色的所有信息
            roles.Name = Name;
            //修改角色名字 
            var rolesmodules = db.RoleModules.Where(x => x.RoleId == Id).ToList();
            //得到当前角色能访问的所有页面id
            for (int i = 0; i < rolesmodules.Count(); i++)
            {
                db.RoleModules.Remove(rolesmodules[i]);
                //先删除当前角色能访问的页面id
            }
            for(int i = 0; i < ModuleId.Count(); i++)
            {
                RoleModules r = new RoleModules();
                r.Id = Guid.NewGuid();
                r.RoleId = Id;
                r.ModuleId = ModuleId[i];
                db.RoleModules.Add(r);
                //添加后来选中的页面
            }
            var count = db.SaveChanges();
            if (count > 0)
            {
                return Json(new { success = true, msg = "修改角色操作成功" });
            }else
            {
                return Json(new { success = false, msg = "修改角色操作失败" });
            }

        }

        /// <summary>
        /// 添加角色页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            {                           
                var allModules = db.Modules.ToList();                
                ViewBag.no = allModules;
            }
            return View();
        }
        /// <summary>
        /// 添加角色方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(string Name, List<int> ModuleId)
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            var relname = db.Roles.SingleOrDefault(x => x.Name == Name);
            if (relname != null)
            {
                return Json(new { success = false, msg = "该角色名已存在" });
            }
            Roles r = new Roles();
            r.Id = Guid.NewGuid();
            r.Name = Name;
            db.Roles.Add(r);
            var count = db.SaveChanges();
            if (count > 0)
            {
                for (int i = 0; i < ModuleId.Count(); i++)
                {
                    RoleModules m = new RoleModules();
                    m.Id = Guid.NewGuid();
                    m.RoleId = r.Id;
                    m.ModuleId = ModuleId[i];
                    db.RoleModules.Add(m);
                    //添加后来选中的页面
                }
                var co = db.SaveChanges();
                if (co > 0)
                {
                    return Json(new { success = true, msg = "添加角色操作成功" });
                }
                else
                {
                    return Json(new { success = false, msg = "添加角色操作失败-1" });
                }
            }
            else
            {
                return Json(new { success = false, msg = "添加角色操作失败-0" });
            }
            
            

        }
    }
}